package edu.faytechcc.csc251.gui;

import edu.faytechcc.csc251.gui.PreviewPanel.ImageListener;
import java.awt.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.*;

/**
 *
 * @author kent
 */
public abstract class Window extends JFrame {

    private JPanel mainPanel;
    private ResultsPanel resultsPanel;
    private JPanel buttonPanel;
    private SearchPanel searchPanel;
    private LogoPanel logoPanel;
    private FilterPanel filterPanel;
    private PreviewPanel previewPanel;
    private JPanel leftPanel;

    public Window() {
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        buildMainPanel();

        this.add(mainPanel);
    }

    private void buildMainPanel() {
        //Create main panel and set a border.
        mainPanel = new JPanel(new BorderLayout());
        mainPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

        //Create the left side
        leftPanel = new JPanel(new BorderLayout());

        //Create the right side
        JPanel rightPanel = new JPanel(new BorderLayout());
        searchPanel = new SearchPanel();
        rightPanel.add(searchPanel, BorderLayout.NORTH);
        JPanel temp = new JPanel(new GridLayout(2, 0));
        logoPanel = new LogoPanel();
        temp.add(logoPanel);
        filterPanel = new FilterPanel();

        temp.add(filterPanel);
        rightPanel.add(temp, BorderLayout.CENTER);
        previewPanel = new PreviewPanel();
        rightPanel.add(previewPanel, BorderLayout.SOUTH);

        //Add everything to the main panel.
        mainPanel.add(leftPanel, BorderLayout.CENTER);
        mainPanel.add(rightPanel, BorderLayout.EAST);

    }

    public void setButtonPanel(JPanel buttonPanel) {
        this.buttonPanel = buttonPanel;
        this.leftPanel.add(this.buttonPanel, BorderLayout.SOUTH);
    }

    public void setResultsPanel(ResultsPanel rPanel) {
        this.resultsPanel = rPanel;
        PreviewPanel.ImageListener imageListener = previewPanel.new ImageListener(previewPanel,
                resultsPanel);
        resultsPanel.addImageListenerToTable(imageListener);
        leftPanel.add(resultsPanel, BorderLayout.CENTER);
        
        FilterButtonListener filterListener = new FilterButtonListener(
                filterPanel.getFilterBoxes(), resultsPanel);
        filterPanel.addActionListenerToFilterButton(filterListener);
        
        SearchButtonListener searchListener = new SearchButtonListener(searchPanel,
                resultsPanel);
        searchPanel.addActionListenerToSearchButton(searchListener);
        
    }

    public void executeSQLStatement(String sql) {
        final String DB_URL =
                "jdbc:derby:MTGCardDB";

        try {
            //Create a connection to the database.
            Connection conn = DriverManager.getConnection(DB_URL);

            //Create a Statement object
            Statement stmt = conn.createStatement();
            stmt.execute(sql);

            //Close the resources
            stmt.close();
            conn.close();
            System.out.println("Action Performed");
        } catch (Exception ex) {
            System.out.println("ERROR: " + ex.getMessage());
        }
    }

    public void executePreparedStatement(int collectionNum) throws SQLException {
        final String DB_URL =
                "jdbc:derby:MTGCardDB";
        Connection conn = null;
        PreparedStatement pstmt = null;
        String deleteString = "DELETE FROM Collection WHERE CollectionNum = ?";
        try {
            conn = DriverManager.getConnection(DB_URL);
            pstmt = conn.prepareStatement(deleteString);
            pstmt.setInt(1, collectionNum);
            pstmt.executeUpdate();
            System.out.println("Card removed from collection");
        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
        } finally {
            if (pstmt != null) {
                pstmt.close();
            }

            if (conn != null) {
                conn.close();
            }
        }
    }
}
